<?php
namespace app\controller;

use app\exception\ApiException;
use app\extensions\lib\Output;
use app\model\Package;
use app\model\TokenInfo;
use support\Log;
use support\Request;
use Webman\RedisQueue\Redis;

class Token
{
    /**
     * 存储 设备token
     * @param Request $request
     * @return \support\Response
     * @throws ApiException
     */
    public function store(Request $request){
        $post = $request->post();
        $this->checkTokenInfo($post);
        $TokenInfo = new TokenInfo();
        $update_fields = $TokenInfo->getFillable();
        $data = array_intersect_key($post,array_combine($update_fields, array_fill(0, count($update_fields), "")));
        $data['ip'] = ipHandle($request->getRealIp());
        Log::channel('request')->info('sdk'.':'.json_encode($data));
        //入队列
        //dump('store:');
        //dump($data);
        $status = intval(Redis::send(TokenInfo::$update_redis_queue_key,$data));
        //$status = 1;
        return Output::success(compact('status'));
    }

    /**
     * 检测参数
     * @param $data
     * @throws ApiException
     */
    protected function checkTokenInfo($data){
        $colomns = ['udid','token','package_id','os_id','channel_id'];
        foreach ($colomns as $colomn){
            if(!isset( $data[$colomn]) || !$data[$colomn]){
                throw new ApiException("param {$colomn} is empty",CODE_PARAM_INVALIDATION);
            }
        }

    }


    public function updateConfig(Request $request){
        $package_id = $request->input('package_id',0);
        $Package = new Package();
        $return = $Package->updateGetConfig($package_id,true);
        return Output::success([
            'package_id' => $package_id,
            'return' => $return,
        ]);

    }



}